var UserData = require('UserData')
cc.Class({
    extends: cc.Component,

    properties: {
        coinPrefab: cc.Prefab
    },
    createCoin() {
        let coin = null;        
        if (this.coinPool.size > 0) {            
            coin= this.coinPool.get();    
        } else {        
            coin = cc.instantiate(this.coinPrefab);
        }        
        return coin
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},


    start () {
        this.time = 0
        this.gravity = cc.v2(0,-30)
        this.decay = 0.3
        this.canvasNode = this.node.parent.parent.parent.parent.parent.parent.parent
        this.coinList = []
        this.coinPool = new cc.NodePool()
        /*setInterval(()=>{
            this.emitCoin()
        },1000)*/
    },
    emitCoin(){
        this.coinExist =true
        this.customerList = this.node.getComponent('Restaurant').customerList
        if(this.customerList){
            for(let j=0;j<this.customerList.length;j++){
                let coin = this.createCoin()
                coin.x = this.customerList[j].x 
                coin.y = this.customerList[j].y + 200
                coin.vx = Math.random() * 1800 -450
                coin.vy = Math.random() *1200 + 500
                //this.node.getChildByName('mask').insertChild(coin)
                coin.zIndex = 102
                this.coinList.push(coin)
                this.node.getChildByName('mask').getChildByName('CustomerCotainer').insertChild(coin)
                coin.on(cc.Node.EventType.TOUCH_START,this.recycleCoin,this)
            }

        }
    },
    recycleCoin(event) {
        //两分钟的加钱
        let money = this.coinList.length * this.node.getComponent('Restaurant').upgradeManager.getCurrentGain() / this.node.getComponent('Restaurant').totalTime * 120 
        UserData.addCurrentMoney(money)
        for(let i=0;i<this.coinList.length;i++){
            let coin = this.coinList[i]
            coin.runAction(cc.callFunc((self,coin)=>{
                coin.runAction(cc.spawn(cc.fadeOut(1).easing(cc.easeExponentialIn()),cc.moveBy(1,cc.v2(0,Math.random() * 100 + 100)).easing(cc.easeExponentialOut())),cc.callFunc(
                    (coin)=>{
                        this.coinPool(coin)
                    },coin))
            },this,coin))
        }
        this.coinList = []
    },
    update (dt) {
        for(let i=0;i<this.coinList.length;i++){
            let coin = this.coinList[i]
            if(coin.x+ coin.vx * dt >this.node.width/2-50 || coin.x+ coin.vx* dt < -this.node.width/2+50){
                coin.vx = -coin.vx * this.decay
                coin.vy = coin.vy * this.decay
            }
            if(coin.y+coin.vy* dt>this.node.height -240 || coin.y+coin.vy* dt< 25 ){
                coin.vy = -coin.vy * this.decay
                coin.vx = coin.vx * this.decay
            }
            coin.x += coin.vx *dt
            coin.y += coin.vy *dt
            this.gravity.x = UserData.runtimeState.gravity.x
            this.gravity.y = UserData.runtimeState.gravity.y
            coin.vy += this.gravity.y
            coin.vx += this.gravity.x
            coin.vy = Math.abs(coin.vy)<10 ?0:coin.vy
            coin.vx = Math.abs(coin.vx)<10 ?0:coin.vx
        }
        if(this.coinExist){
            this.time +=dt
            if(this.time>20){
                this.time = 0
                this.recycleCoin()
            }
        }
    },
});
